capture log close
log 	using 20250823-brfss2015-2019-MI-descriptive, replace text nomsg
version 15.1
clear 	all
macro 	drop _all
set 	linesize 80
set		more off
set		varabbrev off
set 	rmsg on

// #0. Document this do-file and set up packages and directories
	local today : di %tdCY/N/D daily("$S_DATE", "DMY")
	tokenize `today',parse("/")
	local year `1'
	local month `3'
	local day `5'

	local who "Ben Cheung"
	local pgm "20250823-brfss2015-2019-MI-descriptive"
	local date "`year'-`month'-`day'"
	local tag "`pgm'.do by `who' on `date'"
	display "The tag is: `tag'"


	local today : di %tdCYND daily("$S_DATE", "DMY")

	*Check for installed packages
	capture which scheme-plottig.scheme
	if _rc!=0 ssc install blidschemes

	capture which confirmdir
	if _rc!=0 ssc install confirmdir

	capture which estout
	if _rc!=0 ssc install estout

	capture which sum2docx
	if _rc!=0 ssc install sum2docx

	capture which tab2xl
	if _rc!=0 net install http://www.stata.com/users/kcrow/tab2xl, replace

	set scheme plottig




local folder "Data Ready for Analysis"
confirmdir "`folder'"
if `r(confirmdir)'==170 {
	mkdir "`folder'"
}

local folder "graph"
confirmdir "`folder'"
if `r(confirmdir)'==170 {
	mkdir "`folder'"
}

local folder "model"
confirmdir "`folder'"
if `r(confirmdir)'==170 {
	mkdir "`folder'"
}

local folder "summary"
confirmdir "`folder'"
if `r(confirmdir)'==170 {
	mkdir "`folder'"
}


program define pe
	version 15.1
	if `"`0'"' != "" {
		display as input `"`0'"'
		`0'
		display("")
	}
end



// #1. Data management
use "Data Ready for Analysis/brfss2015-MI-pathanalysis-sem-flong.dta",clear //Note that I use the multiply imputed dataset without dropping missing cases of the two health outcomes

mi append using "Data Ready for Analysis/brfss2017-MI-pathanalysis-sem-flong.dta"

mi append using "Data Ready for Analysis/brfss2019-MI-pathanalysis-sem-flong.dta"

mi passive: gen yearsold2=yearsold*yearsold








mi estimate: svy, subpop(if male==0): proportion exe_aerobic_advice

mi estimate: svy, subpop(if male==0): proportion exe_strength_advice

mi estimate: svy, subpop(if male==0): proportion praceethnicgp_multi_sim

mi estimate: svy, subpop(if male==0): mean yearsold

*Reference: https://stats.stackexchange.com/questions/120097/is-it-possible-to-manually-calculate-standard-deviation-for-a-multiply-imputed-s
mi estimate (sd:sqrt(_b[yearsold2] - _b[yearsold]*_b[yearsold])): svy, subpop(if male==0): mean yearsold yearsold2
mi testtransform sd

mi estimate: svy, subpop(if male==0): proportion educ

mi estimate: svy, subpop(if male==0): proportion hhincome

mi estimate: svy, subpop(if male==0): proportion censusregion_sim







mi estimate: svy, subpop(if male==1): proportion exe_aerobic_advice

mi estimate: svy, subpop(if male==1): proportion exe_strength_advice

mi estimate: svy, subpop(if male==1): proportion praceethnicgp_multi_sim

mi estimate: svy, subpop(if male==1): mean yearsold

*Reference: https://stats.stackexchange.com/questions/120097/is-it-possible-to-manually-calculate-standard-deviation-for-a-multiply-imputed-s
mi estimate (sd:sqrt(_b[yearsold2] - _b[yearsold]*_b[yearsold])): svy, subpop(if male==1): mean yearsold yearsold2
mi testtransform sd

mi estimate: svy, subpop(if male==1): proportion educ

mi estimate: svy, subpop(if male==1): proportion hhincome

mi estimate: svy, subpop(if male==1): proportion censusregion_sim







mi estimate: svy: proportion exe_aerobic_advice

mi estimate: svy: proportion exe_strength_advice

mi estimate: svy: proportion praceethnicgp_multi_sim

mi estimate: svy: mean yearsold

*Reference: https://stats.stackexchange.com/questions/120097/is-it-possible-to-manually-calculate-standard-deviation-for-a-multiply-imputed-s
mi estimate (sd:sqrt(_b[yearsold2] - _b[yearsold]*_b[yearsold])): svy: mean yearsold yearsold2
mi testtransform sd

mi estimate: svy: proportion educ

mi estimate: svy: proportion hhincome

mi estimate: svy: proportion censusregion_sim





log close
exit

